package com.wenbai.servicelightluxurystickers.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wenbai.servicelightluxurystickers.entity.StickForum;
import com.wenbai.servicelightluxurystickers.mapper.StickForumMapper;
import com.wenbai.servicelightluxurystickers.mapper.StickForumMemberMapper;
import com.wenbai.servicelightluxurystickers.service.StickForumMemberService;
import com.wenbai.servicelightluxurystickers.service.StickForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class StickForumServiceImpl implements StickForumService {

    @Autowired
    private StickForumMapper stickForumMapper;
    @Autowired
    private StickForumMemberMapper stickForumMemberMapper;
    @Autowired
    private StickForumMemberService stickForumMemberService;

    //    查询贴吧列表
    @Override
    public List<StickForum> selectJoinUser() {
        return stickForumMapper.selectJoinUser();
    }

    @Override
    public List<StickForum> selectByPage(int startPage, int endPage) {
        Page<StickForum> page = new Page<>(startPage, endPage);
        LambdaQueryWrapper<StickForum> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(StickForum::getViewCount);
        IPage<StickForum> result = stickForumMapper.selectPage(page,wrapper);
        return result.getRecords();
    }

    @Override
    @Transactional
    public StickForum insert(StickForum stickForum) {
        stickForum.setCreateTime(null);
        stickForum.setUpdateTime(null);
        stickForumMapper.insert(stickForum);
        StickForum insertForum = stickForumMapper.selectById(stickForum.getForumId());
        stickForumMemberService.insertManage(insertForum.getUserId(),insertForum.getForumId());
        return insertForum;
    }

    @Override
    public boolean update(StickForum stickForum) {
        stickForum.setUpdateTime(null);
        return stickForumMapper.updateById(stickForum) > 0;
    }

    @Override
    @Transactional
    public boolean delete(int id) {
        stickForumMemberMapper.deleteByForumId(id);
        return stickForumMapper.deleteById(id) > 0;
    }

    @Override
    public void setHot(StickForum stickForum, Long userId) {
        stickForumMapper.setHot(stickForum, userId);
    }

    @Override
    public List<StickForum> hotSort() {
        return stickForumMapper.hotSort();
    }
}
